//
//  RTJEditPassWordController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/5.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJEditPassWordController.h"

@interface RTJEditPassWordController ()<UITextFieldDelegate,ZXRegTextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *sendSMSBtn;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet UITextField *codeTxt;
@property (weak, nonatomic) IBOutlet ZXRegTextField *pwTet;

@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
@property (nonatomic,assign) BOOL isShow;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;

@end

@implementation RTJEditPassWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.viewTop.constant = screenH == 812 ? 110 : 90;
    _phoneLab.text = [NSString stringWithFormat:@"%ld",(long)[RTJUserTool getTheUserModel].mobile];
    
    _codeTxt.delegate = self;
    _pwTet.delegate = self;
    _pwTet.zxdelegate = self;
    _pwTet.secureTextEntry = YES;
    _finishBtn.userInteractionEnabled = NO;
    [_finishBtn setBackgroundImage:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    
    
    [self autoSendSMS];
    
}

-(void)autoSendSMS{
    
    NSString *url = _isPayPsd ? @"user/get_pay_password_code":@"user/get_pay_password_code";
    
    [RTJNetWork asyncRequestWithURL:url params:_dic method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                [SVProgressHUD showSuccessWithStatus:@"验证短信已经发送，请注意查收"];
                
                [self.sendSMSBtn startWithTime: 60 title:@"重新获取验证码" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"短信发送失败:%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"短信发送失败:%@",errorMsg]];

        }
        
       
        
        
    }];
}


- (IBAction)sendSMS:(UIButton *)sender {
    
    [self autoSendSMS];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (_codeTxt.text.length != 0 || (_pwTet.text.length>5&&_pwTet.text.length<13)) {
        
        _finishBtn.userInteractionEnabled = YES;
        [_finishBtn setBackgroundImage:[UIImage imageNamed:@"btn_ljcj"] forState: UIControlStateNormal];
        
    }else{
        
        _finishBtn.userInteractionEnabled = NO;
        [_finishBtn setBackgroundImage:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    }
    
}

- (IBAction)showPassWord:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    _isShow = sender.selected;
    
}

- (IBAction)finish:(UIButton *)sender {
    
    if (_codeTxt.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    
    if (_pwTet.text.length<6||_pwTet.text.length>20) {
        
        [SVProgressHUD showErrorWithStatus:@"请设置6-20位的密码"];
        return;
    }
    
    [SVProgressHUD showWithStatus:nil];
    
    NSString *url = _isPayPsd ? @"user/update_pay_password":@"user/update_password";

    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:
                                 @{@"pay_password":_pwTet.text,
                                   @"verify_code":_codeTxt.text
                                   }]; 
    
    [RTJNetWork asyncRequestWithURL:url params:dict method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        
        if (success) {
            
            if ([data[@"status"]integerValue] ==1) {
                
                [SVProgressHUD showSuccessWithStatus:@"设置/修改密码成功"];
                
                RTJUserModel *model = [RTJUserTool getTheUserModel];
                model.paypassword = @"1";
                [RTJUserTool saveUserInfo:model];
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"注册失败:%@",data[@"msg"]]];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
      
    }];
}



@end
